﻿using System;
using System.Data;
using System.Collections;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using AnyOffice.Model;
using FredCK.FCKeditorV2;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.work_plan.manage
{
    public partial class edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

      
        protected void btnAddFile_Click(object sender, EventArgs e)
        {
            if (this.fileAttach.PostedFile.ContentLength > 0)
            {
                try
                {
                    string uniqueTempPath = this.ViewState["UniqueTempPath"].ToString();
                    if (uniqueTempPath == "")
                    {
                        uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                        this.ViewState["UniqueTempPath"] = uniqueTempPath;
                    }
                    Upload upload = new Upload();
                    upload.AutoCreateDirectory = true;
                    upload.OverwriteFile = true;
                    upload.DestinationPath = uniqueTempPath;
                    ConfigManager manager = new ConfigManager();
                    upload.MaxSize = manager.AttachMaxSize;
                    upload.GetFile(this.fileAttach.PostedFile);
                    upload.DoUpload();
                    this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) + upload.FileSize;
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    this.ViewState["FileList"] = list;
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) + 1;
                    this.ltbFileList.Items.Add(Path.GetFileName(this.fileAttach.PostedFile.FileName));
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) != 0)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) > 0)
                    {
                        this.pnlAttach.Visible = true;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件添加", exception);
                    }
                }
            }
        }
        protected void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (this.ltbFileList.Items.Count != 0)
            {
                try
                {
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    list.Remove(this.ltbFileList.SelectedItem.Text);
                    this.ViewState["FileList"] = list;
                    string fileName = this.ViewState["UniqueTempPath"].ToString() + this.ltbFileList.SelectedItem.Text;
                    FileInfo info = new FileInfo(fileName);
                    if (info.Exists)
                    {
                        this.ViewState["TotalSize"] = Convert.ToInt32(this.ViewState["TotalSize"]) - info.Length;
                        if (File.Exists(fileName))
                        {
                            File.Delete(fileName);
                        }
                    }
                    this.ViewState["FileNums"] = Convert.ToInt32(this.ViewState["FileNums"].ToString()) - 1;
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) >= 1)
                    {
                        this.ltbFileList.Rows = Convert.ToInt32(this.ViewState["FileNums"].ToString());
                    }
                    this.ltbFileList.Items.Remove(this.ltbFileList.SelectedItem.Text);
                    if (Convert.ToInt32(this.ViewState["FileNums"].ToString()) == 0)
                    {
                        if (Directory.Exists(this.ViewState["UniqueTempPath"].ToString()))
                        {
                            Directory.Delete(this.ViewState["UniqueTempPath"].ToString());
                        }
                        this.ViewState["UniqueTempPath"] = "";
                        this.pnlAttach.Visible = false;
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("附件删除", exception);
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string attachmentId = "";
            try
            {
                if (this.ViewState["OldAttachmentId"].ToString() != string.Empty)
                {
                    AttachmentBiz.DeleteByAttachmendId(base.Conn, this.ViewState["OldAttachmentId"].ToString());
                }
                if (this.ViewState["UniqueTempPath"].ToString() != "")
                {
                    string uniquePath = AttachmentBiz.GetUniquePath(out attachmentId);
                    Directory.Move(this.ViewState["UniqueTempPath"].ToString(), uniquePath);
                    AttachmentData data = new AttachmentData();
                    AttachmentDao dao = new AttachmentDao();
                    ArrayList list = (ArrayList)this.ViewState["FileList"];
                    for (int i = 0; i < list.Count; i++)
                    {
                        FileInfo info = new FileInfo(uniquePath + list[i].ToString());
                        if (info.Exists)
                        {
                            data.Type = 3;
                            data.AttachmentId = attachmentId;
                            data.Name = info.Name;
                            data.AddTime = DateTime.Now;
                            data.TotalSize = (int)info.Length;
                            data.UserId = base.User.Identity.Name;
                            dao.Insert(base.Conn, data);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("", exception);
                }
            }
            PlanWorkDao dao2 = new PlanWorkDao();
            PlanWorkData data2 = new PlanWorkData();
            data2.PlanId = Convert.ToInt32(this.ViewState["PlanId"]);
            data2.AttachmentId = attachmentId;
            data2.Name = this.txtName.Value;
            data2.Content = this.txtContent.Value;
            if (this.txtBeginDate.Value != "")
            {
                data2.BeginDate = DateTime.Parse(this.txtBeginDate.Value);
            }
            if (this.txtEndDate.Value != "")
            {
                data2.EndDate = DateTime.Parse(this.txtEndDate.Value);
            }
            data2.TypeId = Convert.ToInt32(this.ddlTypeId.SelectedValue);
            data2.ToId = this.ToId.Value;
            data2.Participator = this.ParticipatorId.Value;
            data2.Manager = this.ManagerId.Value;
            data2.AttachmentComment = this.txtAttachmentComment.Value;
            data2.Remark = this.txtRemark.Value;
            data2.Creator = base.User.Identity.Name;
            if (this.rdoStatus0.Checked)
            {
                data2.Status = 0;
            }
            if (this.rdoStatus1.Checked)
            {
                data2.Status = 1;
            }
            if (this.rdoStatus2.Checked)
            {
                data2.Status = 2;
            }
            data2.CompletedPercent = Convert.ToInt32(this.sltCompletedPercent.Value);
            dao2.Update(base.Conn, data2);
            if (this.ckbSms.Checked || this.ckbMobile.Checked)
            {
                string str3 = this.ParticipatorId.Value;
                if (this.ManagerId.Value != string.Empty)
                {
                    str3 = str3 + this.ManagerId.Value;
                }
                if (str3.EndsWith(","))
                {
                    str3 = str3.Substring(0, str3.Length - 1);
                }
                UserCache cache = new UserCache();
                SmsData data3 = new SmsData();
                SmsDao dao3 = new SmsDao();
                data3.Type = 12;
                data3.FromId = base.User.Identity.Name;
                data3.ToDeptList = this.ToId.Value;
                data3.ToIdList = str3;
                data3.Content = cache.UserName + "发布新的项目，请注意查看。";
                data3.RemindFlag = true;
                data3.SendTime = DateTime.Now;
                dao3.BizSend(base.Conn, data3, this.ckbSms.Checked, this.ckbMobile.Checked);
            }
            this.pnlMain.Visible = false;
            this.litMessage.Text = base.ShowPrompt("项目修改成功！", "index1.aspx?page=" + this.ViewState["CurrentPage"].ToString());
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["page"] != null) ? int.Parse(Request.QueryString["page"]) : 1;
                this.ViewState["CurrentPage"] = num;
                int planId = (Request.QueryString["plan_id"] != null) ? int.Parse(Request.QueryString["plan_id"]) : 0;
                this.ViewState["PlanId"] = planId;
                DataTable table = new PlanTypeDao().SelectAll(base.Conn);
                this.ddlTypeId.DataValueField = "TypeId";
                this.ddlTypeId.DataTextField = "TypeName";
                this.ddlTypeId.DataSource = table;
                this.ddlTypeId.DataBind();
                DataTable table2 = new PlanWorkDao().SelectByPlanId(base.Conn, planId);
                if (table2.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此项目", "index1.aspx?page=" + num.ToString());
                }
                else
                {
                    UserCache cache = new UserCache();
                    ConfigManager manager = new ConfigManager();
                    bool flag = cache.PrivId == manager.AdministratorPrivId;
                    if (!(table2.Rows[0]["Creator"].ToString() == base.User.Identity.Name) && !flag)
                    {
                        this.pnlMain.Visible = false;
                        this.litMessage.Text = base.ShowPrompt("无此项目", "index1.aspx?page=" + num.ToString());
                    }
                    else
                    {
                        this.txtName.Value = (table2.Rows[0]["Name"] != DBNull.Value) ? table2.Rows[0]["Name"].ToString() : "";
                        this.txtContent.Value = (table2.Rows[0]["Content"] != DBNull.Value) ? table2.Rows[0]["Content"].ToString() : "";
                        this.txtBeginDate.Value = (table2.Rows[0]["BeginDate"] != DBNull.Value) ? Convert.ToDateTime(table2.Rows[0]["BeginDate"].ToString()).ToString("yyyy-MM-dd") : "";
                        this.txtEndDate.Value = (table2.Rows[0]["EndDate"] != DBNull.Value) ? Convert.ToDateTime(table2.Rows[0]["EndDate"].ToString()).ToString("yyyy-MM-dd") : "";
                        if (this.ddlTypeId.Items.FindByValue(table2.Rows[0]["TypeId"].ToString()) != null)
                        {
                            this.ddlTypeId.Items.FindByValue(table2.Rows[0]["TypeId"].ToString()).Selected = true;
                        }
                        string str = (table2.Rows[0]["ToId"] != DBNull.Value) ? table2.Rows[0]["ToId"].ToString() : "";
                        this.ToId.Value = str;
                        string str2 = (table2.Rows[0]["ToName"] != DBNull.Value) ? table2.Rows[0]["ToName"].ToString() : "";
                        if ((str != "ALL_DEPT") && (str2 != string.Empty))
                        {
                            str2 = str2 + ",";
                        }
                        this.ToName.Value = str2;
                        this.ParticipatorId.Value = (table2.Rows[0]["Participator"] != DBNull.Value) ? table2.Rows[0]["Participator"].ToString() : "";
                        string str3 = (table2.Rows[0]["ParticipatorName"] != DBNull.Value) ? table2.Rows[0]["ParticipatorName"].ToString() : "";
                        if (str3 != string.Empty)
                        {
                            str3 = str3 + ",";
                        }
                        this.ParticipatorName.Value = str3;
                        this.ManagerId.Value = (table2.Rows[0]["Manager"] != DBNull.Value) ? table2.Rows[0]["Manager"].ToString() : "";
                        string str4 = (table2.Rows[0]["ManagerName"] != DBNull.Value) ? table2.Rows[0]["ManagerName"].ToString() : "";
                        if (str4 != string.Empty)
                        {
                            str4 = str4 + ",";
                        }
                        this.ManagerName.Value = str4;
                        this.txtAttachmentComment.Value = (table2.Rows[0]["AttachmentComment"] != DBNull.Value) ? table2.Rows[0]["AttachmentComment"].ToString() : "";
                        this.txtRemark.Value = (table2.Rows[0]["Remark"] != DBNull.Value) ? table2.Rows[0]["Remark"].ToString() : "";
                        switch (Convert.ToInt32(table2.Rows[0]["Status"].ToString()))
                        {
                            case 0:
                                this.rdoStatus0.Checked = true;
                                break;

                            case 1:
                                this.rdoStatus1.Checked = true;
                                break;

                            case 2:
                                this.rdoStatus2.Checked = true;
                                break;
                        }
                        if (this.sltCompletedPercent.Items.FindByValue(table2.Rows[0]["CompletedPercent"].ToString()) != null)
                        {
                            this.sltCompletedPercent.Items.FindByValue(table2.Rows[0]["CompletedPercent"].ToString()).Selected = true;
                        }
                        string attachmentId = (table2.Rows[0]["AttachmentId"] != DBNull.Value) ? table2.Rows[0]["AttachmentId"].ToString() : "";
                        this.ViewState["FileNums"] = 0;
                        this.ViewState["FileList"] = new ArrayList();
                        this.ViewState["UniqueTempPath"] = "";
                        this.ViewState["TotalSize"] = 0;
                        this.btnAddFile.Attributes.Add("onclick", "return CheckFile();");
                        this.ViewState["OldAttachmentId"] = attachmentId;
                        if (attachmentId != string.Empty)
                        {
                            DataTable table3 = new AttachmentDao().SelectByAttachmentId(base.Conn, attachmentId);
                            if (table3.Rows.Count > 0)
                            {
                                try
                                {
                                    this.ViewState["FileNums"] = table3.Rows.Count;
                                    ArrayList list = new ArrayList();
                                    string savedPath = AttachmentBiz.GetSavedPath(attachmentId);
                                    string uniqueTempPath = AttachmentBiz.GetUniqueTempPath();
                                    this.ViewState["UniqueTempPath"] = uniqueTempPath;
                                    AttachmentBiz.Copy(savedPath, uniqueTempPath);
                                    int num4 = 0;
                                    for (int i = 0; i < table3.Rows.Count; i++)
                                    {
                                        list.Add(table3.Rows[i]["Name"].ToString());
                                        num4 += Convert.ToInt32(table3.Rows[i]["TotalSize"].ToString());
                                    }
                                    this.ltbFileList.DataSource = list;
                                    this.ltbFileList.DataBind();
                                    this.ltbFileList.Rows = table3.Rows.Count;
                                    this.ViewState["FileList"] = list;
                                    this.ViewState["TotalSize"] = num4;
                                    this.pnlAttach.Visible = true;
                                }
                                catch (Exception exception)
                                {
                                    if (log.IsErrorEnabled)
                                    {
                                        log.Error("", exception);
                                    }
                                }
                            }
                        }
                        this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                        this.ckbMobile.Visible = SmsDao.IsMobile(SmsDao.SmsType.WorkPlan);
                    }
                }
            }
        }
    }
}
